import matplotlib.pyplot as plt

# 读取第一个 txt 文件的数据
# file1 = 'logs-veri/stage1/resnext101a_384/old_loss/train_log.txt'
# data1_epochs = []
# data1_losses = []
# with open(file1, 'r') as f:
#     for line in f:
#         if 'Epoch[' in line and 'Loss:' in line:
#             parts = line.strip().split()
#             epoch = int(parts[4].split('[')[1][:-1])
#             loss = float(parts[7][:-1])
#             data1_epochs.append(epoch)
#             data1_losses.append(loss)

# # 读取第二个 txt 文件的数据
# file2 = 'logs-veri/stage1/resnext101a_384/new_loss/train_log.txt'
# data2_epochs = []
# data2_losses = []
# with open(file2, 'r') as f:
#     for line in f:
#         if 'Epoch[' in line and 'Loss:' in line:
#             parts = line.strip().split()
#             epoch = int(parts[4].split('[')[1][:-1])
#             loss = float(parts[7][:-1])
#             data2_epochs.append(epoch)
#             data2_losses.append(loss)

# # 绘制折线图
# plt.plot(data1_epochs, data1_losses, label='old loss')
# plt.plot(data2_epochs, data2_losses, label='new loss')
# plt.xlabel('Epoch')
# plt.ylabel('Loss')
# plt.title('Loss')
# plt.legend()
# plt.grid(True)

# # 保存图形到文件
# plt.savefig('2.png')



file1 = 'logs-veri/train/stage1/resnext101a_384/old_loss/train_log.txt'
data1_mAPs = []
with open(file1, 'r') as f:
    for line in f:
        if 'mAP:' in line:
            parts = line.strip().split()
            mAP = float(parts[5][:-1])
            data1_mAPs.append(mAP)

file2 = 'logs-veri/train/stage1/resnext101a_384/new_triplet_loss/train_log.txt'
data2_mAPs = []
with open(file2, 'r') as f:
    for line in f:
        if 'mAP:' in line:
            parts = line.strip().split()
            mAP = float(parts[5][:-1])
            data2_mAPs.append(mAP)

file3 = 'logs-veri/train/stage1/resnext101a_384/MMAL_newtriple_loss/train_log.txt'
data3_mAPs = []
with open(file3, 'r') as f:
    for line in f:
        if 'mAP:' in line:
            parts = line.strip().split()
            mAP = float(parts[5][:-1])
            data3_mAPs.append(mAP)

file4 = 'logs-veri/train/stage1/resnext101a_384/MMAL_oldtriple_loss/train_log.txt'
data4_mAPs = []
with open(file4, 'r') as f:
    for line in f:
        if 'mAP:' in line:
            parts = line.strip().split()
            mAP = float(parts[5][:-1])
            data4_mAPs.append(mAP)

file5 = 'logs-veri/train/stage1/resnext101a_384/MMAL_nomargin_loss/train_log.txt'
data5_mAPs = []
with open(file5, 'r') as f:
    for line in f:
        if 'mAP:' in line:
            parts = line.strip().split()
            mAP = float(parts[5][:-1])
            data5_mAPs.append(mAP)

file6 = 'logs-veri/train/stage1/resnext101a_384/MMAL_nomargin_REA_loss/train_log.txt'
data6_mAPs = []
with open(file6, 'r') as f:
    for line in f:
        if 'mAP:' in line:
            parts = line.strip().split()
            mAP = float(parts[5][:-1])
            data6_mAPs.append(mAP)

file7 = 'logs-veri/train/stage1/resnext101a_384/MMAL_nomargin_ROL_loss/train_log.txt'
data7_mAPs = []
with open(file7, 'r') as f:
    for line in f:
        if 'mAP:' in line:
            parts = line.strip().split()
            mAP = float(parts[5][:-1])
            data7_mAPs.append(mAP)

# 绘制折线图
plt.plot(data1_mAPs, label='oldtriple loss', color='b')
plt.plot(data2_mAPs, label='newtriplet loss')
plt.plot(data3_mAPs, label='MMAL_newtriple_loss')
plt.plot(data4_mAPs, label='MMAL_oldtriple_loss')
plt.plot(data5_mAPs, label='MMAL_nomargin_loss')
plt.plot(data6_mAPs, label='MMAL_nomargin_REA_loss')
plt.plot(data7_mAPs, label='MMAL_nomargin_ROL_loss')
plt.xlabel('Epoch')
plt.ylabel('mAP')
plt.title('mAP')
plt.legend()
plt.grid(True)

# 保存图形到文件
plt.savefig('logs-veri/train/stage1/mAP.png')
